package com.cax.module.tran.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cax.common.constant.Constants;
import com.cax.common.utils.NumberUtils;
import com.cax.common.utils.PageUtils;
import com.cax.module.article.domain.Article;
import com.cax.module.article.domain.vo.ArticleVO;
import com.cax.module.file.domain.FileType;
import com.cax.module.file.domain.vo.File2VO;
import com.cax.module.file.service.FileService;
import com.cax.module.tran.domain.dto.TrainCertificateAddOrUpdDTO;
import com.cax.module.tran.mapper.TrainCertificateMapper;
import com.cax.module.tran.service.TrainCertificateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.cax.module.tran.domain.vo.TrainCertificateVO;
import com.cax.module.tran.domain.dto.TrainCertificatePageQueryDTO;
import com.cax.module.tran.domain.TrainCertificate;
import cn.hutool.core.bean.BeanUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 培训证书管理表 Service实现类
 * @author makejava
 * @date 2024-09-24 11:31:31
 */
@Service
@Slf4j
public class TrainCertificateServiceImpl extends ServiceImpl<TrainCertificateMapper, TrainCertificate> implements TrainCertificateService {

	@Autowired
	TrainCertificateMapper trainCertificateMapper;
	@Autowired
	FileService fileService;

   /**
	 * 查询所有
	 */
	@Override
	public List<TrainCertificateVO> getAll(){
		List<TrainCertificate> list = this.list();
		List<TrainCertificateVO> result = BeanUtil.copyToList(list, TrainCertificateVO.class);
		return result;
	}

	/**
     * 分页查询
     */
	@Override
    public Page<TrainCertificate> getPage(TrainCertificatePageQueryDTO dto){
		//创建分页对象
		Page<TrainCertificate> page = new Page(dto.getPageNum(), dto.getPageSize());
		LambdaQueryWrapper<TrainCertificate> lqw = new LambdaQueryWrapper<>();
		if (StrUtil.isNotBlank(dto.getCertificateName())) {
			lqw.like(true,TrainCertificate::getCertificateName,dto.getCertificateName());
		}
		if (StrUtil.isNotBlank(dto.getName())) {
			lqw.like(true,TrainCertificate::getName,dto.getName());
		}
		if (StrUtil.isNotBlank(dto.getCertificateCode())) {
			lqw.like(true,TrainCertificate::getCertificateCode,dto.getCertificateCode());
		}
		lqw.eq(TrainCertificate::getDeleteFlag, Constants.NO);
		this.page(page, lqw);
		if(CollectionUtils.isEmpty(page.getRecords())){
			return PageUtils.empty(page);
		}
		Page<TrainCertificate> result = PageUtils.entiryTypeConvert(page, TrainCertificate.class);

		return result;
	}

	/**
	 * 通过主键查询
	 */
	@Override
	public TrainCertificateVO getByKey(Integer id){
		TrainCertificate entity = super.getById(id);
		TrainCertificateVO result = BeanUtil.copyProperties(entity, TrainCertificateVO.class);

		//证书图片
		List<File2VO> files = fileService.get(FileType.TypeEnum.FILE_TYPE_4.getCode(),id);
		result.setFiles(files);

        return result;
    }

	@Override
	public List<TrainCertificateVO> getByNameIdentityCard(String name,String identityCard){
		LambdaQueryWrapper<TrainCertificate> lqw = new LambdaQueryWrapper<>();
		lqw.eq(TrainCertificate::getName,name);
		lqw.eq(TrainCertificate::getIdentityCard,identityCard);
		lqw.eq(TrainCertificate::getDeleteFlag,Constants.NO);
		List<TrainCertificate> list = this.list(lqw);
		List<TrainCertificateVO> result = BeanUtil.copyToList(list, TrainCertificateVO.class);
		if(CollectionUtils.isEmpty(result)){
			return result;
		}

		List<Integer> ids = result.stream()
				.map(i -> i.getId())
				.collect(Collectors.toList());
		Map<Integer, List<File2VO>> fileMap = fileService.getMap(FileType.TypeEnum.FILE_TYPE_4.getCode(), ids);
		//数据整合
		for(TrainCertificateVO i:result){
			if(fileMap.containsKey(i.getId())){
				i.setFiles(fileMap.get(i.getId()));
			}else{
				i.setFiles(new ArrayList<>());
			}
		}

		return  result;
	}


	/**
	 * 添加
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer add(TrainCertificateAddOrUpdDTO dto){
		TrainCertificate trainCertificate = BeanUtil.copyProperties(dto, TrainCertificate.class);
		trainCertificate.setId(null);
		this.save(trainCertificate);

		//证书图片
		//工单文件处理
		if(!CollectionUtils.isEmpty(dto.getFileIds())){
			fileService.addSave(trainCertificate.getId(),dto.getFileIds());
		}

		return trainCertificate.getId();
	}

	/**
	 * 通过主键修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upd(TrainCertificateAddOrUpdDTO dto){
		TrainCertificate trainCertificate = BeanUtil.copyProperties(dto, TrainCertificate.class);
	    this.updateById(trainCertificate);

		//证书图片
		//工单文件处理
		fileService.updSave(FileType.TypeEnum.FILE_TYPE_4.getCode(),trainCertificate.getId(),dto.getFileIds());
	}

	@Override
	public void del(Integer id){
		TrainCertificate trainCertificate = new TrainCertificate();
		trainCertificate.setId(id);
		trainCertificate.setDeleteFlag(Constants.YES);
		this.updateById(trainCertificate);
	}
}



